#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QGridLayout>
#include <QSizePolicy>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //创建六个按钮
    QPushButton *button1 =new QPushButton("按钮1");
    QPushButton *button2 =new QPushButton("按钮2");
    QPushButton *button3 =new QPushButton("按钮3");
    QPushButton *button4 =new QPushButton("按钮4");
    QPushButton *button5 =new QPushButton("按钮5");
    QPushButton *button6 =new QPushButton("按钮6");

    //创建一个网格布局管理器
    QGridLayout *layout = new QGridLayout();
    //这里的行和列，只是并不是具体的行列，只是一个相对的数字
    //layout->addWidget(button1,0,0);
    //layout->addWidget(button2,1,1);
    //layout->addWidget(button3,2,2);
    //layout->addWidget(button4,3,3);
    //layout->addWidget(button5,4,4);
    //layout->addWidget(button6,100,100);

    layout->addWidget(button1,0,0);
    layout->addWidget(button2,0,1);
    layout->addWidget(button3,0,2);
    layout->addWidget(button4,1,0);
    layout->addWidget(button5,1,1);
    layout->addWidget(button6,1,2);

    //设置按钮的水平和垂直方向，都尽量舒展开
    button1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button3->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button4->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button5->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button6->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    //设置拉伸系数 第一个参数为列数，第二个参数为显示比例
    layout->setColumnStretch(0,1);
    layout->setColumnStretch(1,2);
    layout->setColumnStretch(2,0);//设置为0，代表这一列不参与拉伸

    //设置RowStretch的效果不明显，因为每个按钮的高度是固定的
    //需要先设置按钮的sizePolicy属性
    layout->setRowStretch(0,2);
    layout->setRowStretch(1,1);

    //设置边距
    layout->setContentsMargins(100,10,10,10);

    this->setLayout(layout);
}

Widget::~Widget()
{
    delete ui;
}

